package wtws.message.detail;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import wtws.util.ByteUtil;

public class AlphanumericProductSubBlock {
	public static final Map<String, Integer> ARENA_LOCATION = new HashMap<String, Integer>();
	static {
		ARENA_LOCATION.put("NO_ARENA", 0);
		ARENA_LOCATION.put("3MD", 1);
		ARENA_LOCATION.put("2MD", 2);
		ARENA_LOCATION.put("1MD", 3);
		ARENA_LOCATION.put("RWY", 4);
		ARENA_LOCATION.put("1MF", 5);
		ARENA_LOCATION.put("2MF", 6);
		ARENA_LOCATION.put("3MF", 7);
	}

	public static final int MSG_LEN = 7;

	private static final byte SPACE = (byte) ' ';
	private int alphanumericMsgLength;
	private int arenaLocation;
	private int alphanMsgType;
	private int alphanMsgId;
	private char alphanRunwayLocation;
	private String anMsgChar;

	public AlphanumericProductSubBlock(int arenaLocation, int alphanMsgType, int alphanMsgId, char alphanRunwayLocation, String anMsgChar) {
		this.arenaLocation = arenaLocation;
		this.alphanMsgType = alphanMsgType;
		this.alphanMsgId = alphanMsgId;
		this.alphanRunwayLocation = alphanRunwayLocation;
		this.anMsgChar = anMsgChar;
		this.alphanumericMsgLength = anMsgChar.length();
	}

	public byte[] toByteArray() {
		int size = MSG_LEN;
		if (alphanumericMsgLength % 2 == 0)
			size += alphanumericMsgLength + 1;
		else
			size += alphanumericMsgLength;
		ByteBuffer br = ByteBuffer.allocate(size);
		br.put(ByteUtil.to2Bytes(alphanumericMsgLength));
		br.put(ByteUtil.toByte(arenaLocation));
		br.put(ByteUtil.EMPTY_1B);
		br.put(ByteUtil.toByte(alphanMsgType));
		br.put(ByteUtil.toByte(alphanMsgId));
		br.put(ByteUtil.toByte(alphanRunwayLocation));
		for (int i = 0; i < alphanumericMsgLength; i++) {
			br.put((byte) anMsgChar.charAt(i));
		}
		if (alphanumericMsgLength % 2 == 0)
			br.put(SPACE);
		return br.array();
	}
	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append(getAlphanMsgId());
		buf.append(",");
		buf.append(getArenaLocation());
		buf.append(",");
		buf.append(getAnMsgChar());
		return buf.toString();
	}

	public int getAlphanumericMsgLength() {
		return alphanumericMsgLength;
	}

	public void setAlphanumericMsgLength(int alphanumericMsgLength) {
		this.alphanumericMsgLength = alphanumericMsgLength;
	}

	public int getArenaLocation() {
		return arenaLocation;
	}

	public void setArenaLocation(int arenaLocation) {
		this.arenaLocation = arenaLocation;
	}

	public int getAlphanMsgType() {
		return alphanMsgType;
	}

	public void setAlphanMsgType(int alphanMsgType) {
		this.alphanMsgType = alphanMsgType;
	}

	public int getAlphanMsgId() {
		return alphanMsgId;
	}

	public void setAlphanMsgId(int alphanMsgId) {
		this.alphanMsgId = alphanMsgId;
	}

	public char getAlphanRunwayLocation() {
		return alphanRunwayLocation;
	}

	public void setAlphanRunwayLocation(char alphanRunwayLocation) {
		this.alphanRunwayLocation = alphanRunwayLocation;
	}

	public String getAnMsgChar() {
		return anMsgChar;
	}

	public void setAnMsgChar(String anMsgChar) {
		this.anMsgChar = anMsgChar;
	}

	public static int getMsgLen() {
		return MSG_LEN;
	}


}
